#version 300 es
precision mediump float;

in vec4 v_color;
in vec2 v_texCoord;
in vec2 v_viewPosition2d;

uniform sampler2D u_texture;
uniform sampler2D u_lightDataTexture;
uniform float u_time;  // 时间变量
uniform float u_dayTime;//一整天时长: 秒
uniform vec2 u_viewSize;//视口宽高
uniform int u_lightAmount;//有效光源数量
uniform float u_lightIntensityCompressVal;//光源强度数据压缩

out vec4 fragColor;

// 定义昼夜颜色配置
#define DAY_COLOR vec3(1.0, 0.98, 0.9)    // 浅黄色（白天）
#define NIGHT_COLOR vec3(0.04, 0.06, 0.12)   // 深蓝色（夜晚）
#define DUSK_COLOR vec3(0.8, 0.4, 0.2)    // 黄昏橙红色
#define pixelPerUnit 8.0//定义像素化单位大小
#define whiteColor vec4(1.0)//白色


//环境光
vec3 calculateAmbientColor(vec4 texColor) {
    float adjustedTime = u_time / u_dayTime;
    // 计算时间曲线（平滑过渡）1秒为sin的一圈
    float timeCurve = sin(adjustedTime * 3.14159265 * 2.0) * 0.5 + 0.5;

    // 混合昼夜颜色
    vec3 dayLightColor = mix(NIGHT_COLOR, DAY_COLOR, smoothstep(0.2, 0.8, timeCurve));
    vec3 pass1Color = clamp(texColor.rgb * dayLightColor.rgb, 0.0, 1.0);

    return pass1Color;
}


//视口宽高比
float getViewWidthRatio() {
    return u_viewSize.x / u_viewSize.y;
}

//光源数据采样
vec4 sampleExactPixel(float pixelCoord, sampler2D sampleTexture) {
    ivec2 texSize = textureSize(sampleTexture, 0);
    vec2 texCoord = (pixelCoord + 0.5) / vec2(texSize); // +0.5采样像素中心
    return texture(sampleTexture, texCoord);
}

//单个光影响度
float calculateOnePointPixelatedIntensity(vec2 fragPos2d, vec2 lightPos2d) {

    // 计算像素化后的距离（考虑宽高比修正）
    vec2 delta = (fragPos2d - lightPos2d) / u_viewSize;
    delta.x *= getViewWidthRatio();
    float dist = length(delta);

    return dist;
}

//总光源影响度
float caculatePixelatedIntensity() {
    vec2 fragPixelPos = v_viewPosition2d * u_viewSize;
    vec2 pixelatedFragPos = floor(fragPixelPos / pixelPerUnit) * pixelPerUnit;

    int maxIndex = -1;
    float maxIntensity = 0.0;
    for(int i=0;i < u_lightAmount;i++) {
        vec4 curLightData = sampleExactPixel(vec2(float(i), 0), u_lightDataTexture);

        vec2 lightPosition2d = curLightData.xy;
        float lightIntensity = curLightData.a * u_lightIntensityCompressVal;

        vec2 lightPixelPos = lightPosition2d * u_viewSize;
        vec2 pixelatedLightPos = floor(lightPixelPos / pixelPerUnit) * pixelPerUnit;

        float dist = calculateOnePointPixelatedIntensity(pixelatedFragPos, pixelatedLightPos);
        float lightSpread = 0.15 * pow(lightIntensity, 0.4);//3参数分别为: 基础大小, 光强, 光强对大小影响度
        //线性衰减
        float intensity = (lightSpread - dist) / lightSpread;
//        float intensity = 1.0 - dist;

        float onePointSpreadedIntensity = intensity;

        float onePointLightIntensity = onePointSpreadedIntensity * lightIntensity;

//        //选择影响度最强的
//        if(onePointLightIntensity > maxIntensity){
//            maxIndex = i;
//            maxIntensity = onePointLightIntensity;
//        }
        //所有影响度相加
        if(onePointLightIntensity > 0.0){
            maxIndex = i;
            maxIntensity += onePointLightIntensity;
        }
    }

    if(maxIndex == -1) return 0.0;
    return maxIntensity;
}

void main() {
    vec4 texColor = texture(u_texture, v_texCoord) * v_color;

    //跳过透明像素
    if(texColor.a <= 0.0){
    	fragColor = texColor;
    	return;
    }

    vec3 finalColor = vec3(0.0);

    //计算环境光
    vec3 pass1Color = calculateAmbientColor(texColor);

    //计算点光源
    float lightIntensity = caculatePixelatedIntensity();
    vec4 lightColor = vec4(1.0);//暂时白色
    float finalLightIntensity = lightIntensity;

    //混合环境光与点光源
    if(finalLightIntensity > 0.0){
    	vec3 lightenedColor = texColor.rgb * finalLightIntensity;//增益原材质
//    	vec3 lightenedColor = lightColor.rgb * finalLightIntensity * 0.3;//光源颜色
//		vec3 pass2Color = mix(pass1Color, lightenedColor, finalLightIntensity * 0.3);//混合
		vec3 pass2Color = pass1Color + lightenedColor;//覆盖
		finalColor = pass2Color;
    }
    else
        finalColor = pass1Color;

    //最终颜色
    fragColor = vec4(finalColor, texColor.a);
}
